home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lib-src / hexl.c < prev    next >
C/C++ Source or Header  |  1993-11-16  |  4KB  |  220 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define DEFAULT_GROUPING    0x01
  5. #define DEFAULT_BASE        16
  6.  
  7. #undef TRUE
  8. #undef FALSE
  9. #define TRUE  (1)
  10. #define FALSE (0)
  11.  
  12. extern void exit(), perror();
  13.  
  14. int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
  15. int group_by = DEFAULT_GROUPING;
  16. char *progname;
  17.  
  18. main(argc, argv)
  19. int argc;
  20. char *argv[];
  21. {
  22.     register long address;
  23.     char string[18];
  24.     FILE *fp;
  25.  
  26.     progname = *argv++; --argc;
  27.  
  28.     /*
  29.     ** -hex        hex dump
  30.     ** -oct        Octal dump
  31.     ** -group-by-8-bits
  32.     ** -group-by-16-bits
  33.     ** -group-by-32-bits
  34.     ** -group-by-64-bits
  35.     ** -iso        iso character set.
  36.     ** -big-endian    Big Endian
  37.     ** -little-endian    Little Endian
  38.     ** -un || -de    from hexl format to binary.
  39.     ** --        End switch list.
  40.     ** <filename>    dump filename
  41.     ** -        (as filename == stdin)
  42.     */
  43.     
  44.     while (*argv && *argv[0] == '-' && (*argv)[1])
  45.     {
  46.     /* A switch! */
  47.     if (!strcmp(*argv, "--"))
  48.     {
  49.         --argc; argv++;
  50.         break;
  51.     } else if (!strcmp(*argv, "-un") || !strcmp(*argv, "-de"))
  52.     {
  53.         un_flag = TRUE;
  54.         --argc; argv++;
  55.     } else if (!strcmp(*argv, "-hex"))
  56.     {
  57.         base = 16;
  58.         --argc; argv++;
  59.     } else if (!strcmp(*argv, "-iso"))
  60.     {
  61.         iso_flag = TRUE;
  62.         --argc; argv++;
  63.     } else if (!strcmp(*argv, "-oct"))
  64.     {
  65.         base = 8;
  66.         --argc; argv++;
  67.     } else if (!strcmp(*argv, "-big-endian"))
  68.     {
  69.         endian = 1;
  70.         --argc; argv++;
  71.     } else if (!strcmp(*argv, "-little-endian"))
  72.     {
  73.         endian = 0;
  74.         --argc; argv++;
  75.     } else if (!strcmp(*argv, "-group-by-8-bits"))
  76.     {
  77.         group_by = 0x00;
  78.         --argc; argv++;
  79.     } else if (!strcmp(*argv, "-group-by-16-bits"))
  80.     {
  81.         group_by = 0x01;
  82.         --argc; argv++;
  83.     } else if (!strcmp(*argv, "-group-by-32-bits"))
  84.     {
  85.         group_by = 0x03;
  86.         --argc; argv++;
  87.     } else if (!strcmp(*argv, "-group-by-64-bits"))
  88.     {
  89.         group_by = 0x07;
  90.         endian = 0;
  91.         --argc; argv++;
  92.     } else
  93.     {
  94.         (void) fprintf(stderr, "%s: invalid switch: \"%s\".\n", progname,
  95.                *argv);
  96.         usage();
  97.     }
  98.     }
  99.  
  100.     do
  101.     {
  102.     if (*argv == NULL)
  103.         fp = stdin;
  104.     else
  105.     {
  106.         char *filename = *argv++;
  107.  
  108.         if (!strcmp(filename, "-"))
  109.         fp = stdin;
  110.         else
  111.         if ((fp = fopen(filename, "r")) == NULL)
  112.         {
  113.             perror(filename);
  114.             continue;
  115.         }
  116.     }
  117.  
  118.     if (un_flag)
  119.     {
  120.         char buf[18];
  121.  
  122.         for (;;)
  123.         {
  124.         register int i, c, d;
  125.  
  126. #define hexchar(x) (isdigit(x) ? x - '0' : x - 'a' + 10)
  127.  
  128.         (void) fread(buf, 1, 10, fp); /* skip 10 bytes */
  129.  
  130.         for (i=0; i < 16; ++i)
  131.         {
  132.             if ((c = getc(fp)) == ' ' || c == EOF)
  133.             break;
  134.  
  135.             d = getc(fp);
  136.             c = hexchar(c) * 0x10 + hexchar(d);
  137.             (void) putchar(c);
  138.  
  139.             if ((i&group_by) == group_by)
  140.             (void) getc(fp);
  141.         }
  142.  
  143.         if (c == ' ')
  144.         {
  145.             while ((c = getc(fp)) != '\n' && c != EOF)
  146.             ;
  147.  
  148.             if (c == EOF)
  149.             break;
  150.         }
  151.         else
  152.         {
  153.             if (i < 16)
  154.             break;
  155.  
  156.             (void) fread(buf, 1, 18, fp); /* skip 18 bytes */
  157.         }
  158.         }
  159.     }
  160.     else
  161.     {
  162.         address = 0;
  163.         string[0] = ' ';
  164.         string[17] = '\0';
  165.         for (;;)
  166.         {
  167.         register int i, c;
  168.  
  169.         for (i=0; i < 16; ++i)
  170.         {
  171.             if ((c = getc(fp)) == EOF)
  172.             {
  173.             if (!i)
  174.                 break;
  175.  
  176.             (void) fputs("  ", stdout);
  177.             string[i+1] = '\0';
  178.             }
  179.             else
  180.             {
  181.             if (!i)
  182.                 (void) printf("%08x: ", address);
  183.  
  184.             if (iso_flag)
  185.                 string[i+1] =
  186.                 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
  187.             else
  188.                 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
  189.  
  190.             (void) printf("%02x", c);
  191.             }
  192.  
  193.             if ((i&group_by) == group_by)
  194.             (void) putchar(' ');
  195.         }
  196.  
  197.         if (i)
  198.             (void) puts(string);
  199.  
  200.         if (c == EOF)
  201.             break;
  202.  
  203.         address += 0x10;
  204.  
  205.         }
  206.     }
  207.  
  208.     if (fp != stdin)
  209.         (void) close(fp);
  210.  
  211.     } while (*argv != NULL);
  212.     return 0;
  213. }
  214.  
  215. usage()
  216. {
  217.     (void) fprintf(stderr, "usage: %s [-de] [-iso]\n", progname);
  218.     exit(1);
  219. }
  220.